<?php
namespace Admin\Controller;
use Think\Controller;
/*
 * 街道、社区、文章管理
 */
class StreetController extends CommonController {
    //文章列表页展示
    public function index()
    {
        $table = 'com_introduce';
        $field = 'id,title,content,read_num,praise,update_time';
        $where = 'deletes = 1';

        if(!isset($_GET['p'])) {
            $_GET['p'] = 1;
        }

        // 实例化User对象
        $User = M($table);
        // 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
        $list = $User->field($field)->where($where)->order('id desc')->page($_GET['p'].',10')->select();
        // 赋值分页输出
        $this->assign('introduce',$list);

        // 查询满足要求的总记录数
        $count      = $User->where($where)->count();

        // 实例化分页类 传入总记录数和每页显示的记录数
        $Page       = new \Think\Page($count,10);

        $Page -> setConfig('header','共%TOTAL_ROW%条');
        $Page -> setConfig('first','首页');
        $Page -> setConfig('last','共%TOTAL_PAGE%页');
        $Page -> setConfig('prev','上一页');
        $Page -> setConfig('next','下一页');
        $Page -> setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');

        $show       = $Page->show();

        // 分页显示输出
        $this->assign('page',$show);

        $this->display(); // 输出模板


//        $table = 'com_introduce';
//        $field = 'id,title,content,read_num,praise,update_time';
//        $where = 'deletes = 1';
//        $introduce = D('Street') -> dataInfo($table,$field,$where);
//        $this->assign('introduce',$introduce);
//        $this -> display();
    }
    //添加页显示
    public function add()
    {
        $table = 'street';
        $field = 'id,name';
        $where = 'deletes = 1';
        $street = D('Street') -> dataInfo($table,$field,$where);
        $c_table = 'community';
        $c_field = 'id,name';
        $where = 'deletes = 1';
        $comm = D('Street') -> datainfo($c_table,$c_field,$where);
        $this->assign('comm',$comm);
        $this->assign('street',$street);
        $this -> display();
    }
    //街道添加
    public function str_add()
    {
        $data = $_POST;
        if ( $data['name'] ) {
            $street = M('Street');
            $add = $street -> add($data);
            if($add){
                $info['title'] ="恭喜您！";
                $info['text'] = "恭喜您，添加成功！";
                $info['type'] = "success";
            }else{
                $info['title'] ="对不起！";
                $info['text'] = "对不起，添加失败！";
                $info['type'] = "error";
            }
        } else {
            $info['title'] ="对不起！";
            $info['text'] = "添加失败，请填写街道名称！";
            $info['type'] = "error";
        }
        $this -> ajaxReturn($info);
    }
    //社区添加
    public function com_add()
    {
        $community = M('community');
        $data['name'] = $_POST['com_name'];
        $data['street_id'] = $_POST['street_id'];
        if($data){
            $add = $community -> add($data);
            if($add){
                $info['title'] ="恭喜您！";
                $info['text'] = "社区添加成功！";
                $info['type'] = "success";
            }else{
                $info['title'] ="对不起！";
                $info['text'] = "街道添加失败！";
                $info['type'] = "error";
            }
        }else{
            $info['title'] ="对不起！";
            $info['text'] = "添加失败，请重新填写信息！";
            $info['type'] = "error";
        }
        $this -> ajaxReturn($info);
    }
    //文章添加
    public function art_add(){
        $introduce = M('com_introduce');
        $data = $_POST;
        $data['create_time'] = date("Y-m-d H:i:s",time());
        $data['update_time'] = date("Y-m-d H:i:s",time());
        if($data){
            $add = $introduce -> add($data);
            if($add){
                $info['title'] ="恭喜您！";
                $info['text'] = "文章添加成功！";
                $info['type'] = "success";
            }else{
                $info['title'] ="对不起！";
                $info['text'] = "文章添加失败！";
                $info['type'] = "error";
            }
        }else{
            $info['title'] ="对不起！";
            $info['text'] = "添加失败，请重新填写信息！";
            $info['type'] = "error";
        }
        $this -> ajaxReturn($info);
    }

    //街道、社区列表及修改页面
    public function upd()
    {
        $this->display();
    }

    // 获取社区信息
    public function getStreet()
    {
        $sortField = I('get.sidx');    // 排序字段
        $sortType  = I('get.sord');    // 排序方式
        $rows      = I('get.rows');    // 显示行数
        $page      = I('get.page');    // 显示页数
        $filters   = json_decode($_GET['filters']); // 查询条件

        // 初始化条件变量
        $where = null;
        // 拼装条件数组
        foreach ($filters->rules as $k => $v) {
            // 转换条件符号
            switch ($v->op) {
                case 'cn':
                    $v->op = 'LIKE';
                    $v->data = "%{$v->data}%";
                    break;
                case 'le':
                    $v->op = 'ELT';
                    break;
                case 'ge':
                    $v->op = 'EGT';
                    break;
            }

            // 判断是否存在字段
            if (array_key_exists($v->field, $where)) {
                // 初始化临时条件变量数组
                $whereInfo = array();
                // 获取条件并拼装
                $whereInfo[] = array($where[$v->field][0], $where[$v->field][1]);
                // 获取条件并拼装
                $whereInfo[] = array($v->op, $v->data);
                // 获取条件并拼装
                $whereInfo[] = $filters->groupOp;
                // 判断是否为时间字段并转换成时间戳
                if ($v->field == 'time') {
                    foreach ($whereInfo as $wherek => $wherev) {
                        if(is_array($wherev)) {
                            $whereInfo[$wherek][1] = strtotime($wherev[1]);
                        }
                    }

                }
                // 装载查询条件
                $where[$v->field] = $whereInfo;
            } else {
                // 单条件时判断是否为时间字段并转换成时间戳
                if($v->field == 'time') {
                    $v->data = strtotime($v->data);
                }
                // 装载查询条件
                $where[$v->field] = array($v->op, $v->data);
            }
        }
        // 限制最大行数
        if ($rows > 30) {
            $rows = 30;
        }
        // 实例化三会一课Model类
        $model = D('Street');

        // 获取三会一课文章列表数量
        $sum   = $model->getListSum('id', $where);

        // 计算变量最大页数
        $maxPage = ceil($sum/$rows);
        // 最大页数限制
        if ($page > $maxPage) {
            $page = $maxPage;
        }
        //

        // 数据查询
        $listData  = $model->getInfo('*', $where, ($page-1)*$rows, $rows, $sortField, $sortType);
        // 初始化数据变量及装载数据
        $data = array();
        $data['page'] = $page;
        $data['total'] = $maxPage;
        $data['records'] = $rows;
//        // 转换日期 (前端转换时间戳，前端转就打开注释)
//        foreach($listData as $k => $v) {
//            $listData[$k]['time'] = date('Y-m-d h:i:s',$v['time']);
//        }
        $data['list'] = $listData;
        // 数据返回
        $this->ajaxReturn($data);
    }

    // 社区修改
    public function streetEdit()
    {
        $model = D('Street');
        $data = I('post.');
        $bool = $model->edit($data);


        if ($bool) {
            $message = array(
                'title' => '操作成功!',
                'intro' => '您的信息已经修改成功！'
            );
            $statu = 'success';
            $array = array(
                'status'  => $statu,
                'message' => $message
            );

        } else {
            $message = array(
                'title' => '操作失败!',
                'intro' => '请检查您的数据是否合法或数据是否已经有修改！'
            );
            $statu = 'error';
            $array = array(
                'status'  => $statu,
                'message' => $message
            );
        }
        $this->ajaxReturn($array);
    }

    // 社区删除
    public function  streetDelete()
    {
        // 实例化三会一课Model类
        $model = D('Street');
        $data  = I('post.');
        $bool = $model->delete($data);

        if ($bool) {
            $message = array(
                'title' => '操作成功!',
                'intro' => '您的信息已经删除成功！'
            );
            $statu = 'success';
            $array = array(
                'status'  => $statu,
                'message' => $message
            );

        } else {
            $message = array(
                'title' => '操作失败!',
                'intro' => '请检查您的数据是否合法或数据是否已经有删除！'
            );
            $statu = 'error';
            $array = array(
                'status'  => $statu,
                'message' => $message
            );
        }
        $this->ajaxReturn($array);
    }

    // 获取社区信息
    public function getCommunity()
    {
        $sortField = I('get.sidx');    // 排序字段
        $sortType  = I('get.sord');    // 排序方式
        $rows      = I('get.rows');    // 显示行数
        $page      = I('get.page');    // 显示页数
        $filters   = json_decode($_GET['filters']); // 查询条件

        // 初始化条件变量
        $where = null;
        // 拼装条件数组
        foreach ($filters->rules as $k => $v) {
            // 转换条件符号
            switch ($v->op) {
                case 'cn':
                    $v->op = 'LIKE';
                    $v->data = "%{$v->data}%";
                    break;
                case 'le':
                    $v->op = 'ELT';
                    break;
                case 'ge':
                    $v->op = 'EGT';
                    break;
            }

            // 判断是否存在字段
            if (array_key_exists($v->field, $where)) {
                // 初始化临时条件变量数组
                $whereInfo = array();
                // 获取条件并拼装
                $whereInfo[] = array($where[$v->field][0], $where[$v->field][1]);
                // 获取条件并拼装
                $whereInfo[] = array($v->op, $v->data);
                // 获取条件并拼装
                $whereInfo[] = $filters->groupOp;
                // 判断是否为时间字段并转换成时间戳
                if ($v->field == 'time') {
                    foreach ($whereInfo as $wherek => $wherev) {
                        if(is_array($wherev)) {
                            $whereInfo[$wherek][1] = strtotime($wherev[1]);
                        }
                    }

                }
                // 装载查询条件
                $where[$v->field] = $whereInfo;
            } else {
                // 单条件时判断是否为时间字段并转换成时间戳
                if($v->field == 'time') {
                    $v->data = strtotime($v->data);
                }
                // 装载查询条件
                $where[$v->field] = array($v->op, $v->data);
            }
        }
        // 限制最大行数
        if ($rows > 30) {
            $rows = 30;
        }
        // 实例化三会一课Model类
        $model = D('Community');

        // 获取三会一课文章列表数量
        $sum   = $model->getListSum('id', $where);

        // 计算变量最大页数
        $maxPage = ceil($sum/$rows);
        // 最大页数限制
        if ($page > $maxPage) {
            $page = $maxPage;
        }
        //

        // 数据查询
        $listData  = $model->getInfo('*', $where, ($page-1)*$rows, $rows, $sortField, $sortType);
        // 初始化数据变量及装载数据
        $data = array();
        $data['page'] = $page;
        $data['total'] = $maxPage;
        $data['records'] = $rows;
//        // 转换日期 (前端转换时间戳，前端转就打开注释)
//        foreach($listData as $k => $v) {
//            $listData[$k]['time'] = date('Y-m-d h:i:s',$v['time']);
//        }
        $data['list'] = $listData;
        // 数据返回
        $this->ajaxReturn($data);
    }

    // 社区修改
    public function communityEdit()
    {
        $model = D('Community');
        $data = I('post.');
        $bool = $model->edit($data);


        if ($bool) {
            $message = array(
                'title' => '操作成功!',
                'intro' => '您的信息已经修改成功！'
            );
            $statu = 'success';
            $array = array(
                'status'  => $statu,
                'message' => $message
            );

        } else {
            $message = array(
                'title' => '操作失败!',
                'intro' => '请检查您的数据是否合法或数据是否已经有修改！'
            );
            $statu = 'error';
            $array = array(
                'status'  => $statu,
                'message' => $message
            );
        }
        $this->ajaxReturn($array);
    }

    // 社区删除
    public function  communityDelete()
    {
        // 实例化三会一课Model类
        $model = D('Community');
        $data  = I('post.');
        $bool = $model->delete($data);

        if ($bool) {
            $message = array(
                'title' => '操作成功!',
                'intro' => '您的信息已经删除成功！'
            );
            $statu = 'success';
            $array = array(
                'status'  => $statu,
                'message' => $message
            );

        } else {
            $message = array(
                'title' => '操作失败!',
                'intro' => '请检查您的数据是否合法或数据是否已经有删除！'
            );
            $statu = 'error';
            $array = array(
                'status'  => $statu,
                'message' => $message
            );
        }
        $this->ajaxReturn($array);
    }

    //Ajax获取街道信息
    public function ajax_street()
    {
        $table = 'street';
        $field = 'id,name';
        $street = D('Street') -> dataInfo($table,$field);
        $this -> ajaxReturn($street);
    }
    //文章修改
    public function edit()
    {
        $id = $_GET['id'];
        $data = $_POST;
        if(!empty($data)){
            $introduce = M('com_introduce');
            $data['update_time'] = date("Y-m-d H:i:s",time());
            if($data){
                $add = $introduce ->where('id = '.$data['id'])-> field('title,content,update_time') -> save($data);
                if($add){
                    $info['title'] ="恭喜您！";
                    $info['text'] = "修改成功！";
                    $info['type'] = "success";
                }else{
                    $info['title'] ="对不起！";
                    $info['text'] = "修改失败！";
                    $info['type'] = "error";
                }
            }else{
                $info['title'] ="对不起！";
                $info['text'] = "修改失败，请重新填写信息！";
                $info['type'] = "error";
            }
            $this -> ajaxReturn($info);
        }else{
            //社区显示
            $c_table = 'community';
            $c_field = 'id,name';
            $comm = D('Street') -> datainfo($c_table,$c_field);
            //文章表显示
            $table = 'com_introduce as i';
            $field = 'i.id,i.com_id,i.title,i.content,c.name';
            $where = 'i.id = '.$id;
            $join = 'hd_community as c on i.com_id = c.id';
            $intro = M($table) ->join($join) -> field($field) -> where($where) -> find();
            $this->assign('intro',$intro);
            $this->assign('comm',$comm);
            $this->display();
        }

    }
    //文章删除
    public function intro_del()
    {
        $id = I('post.id');
        $intro = M('com_introduce');
        $data['deletes'] = 2;
        $rs = $intro -> where("id = $id") -> save($data);
        if($rs){
            $this->ajaxReturn("success");
        } else {
            $this->ajaxReturn("error");
        }

    }


}